package  
{
	import flash.events.Event;
	/**
	 * ...
	 * @author ...
	 */
	public class ClMeteor extends ClObject 
	{
		public static var TYPE_SMALL:String = "type_small";
		public static var TYPE_NORMAL:String = "type_normal";
		public static var TYPE_BIG:String = "type_big";
		
		public static var MeteorArray:Array = new Array();
		public var rotationSpeed:Number = 0;
		
		public static var SizeType:String = TYPE_NORMAL;
		
		//public static var RandomiseX:Boolean = true
		public static var UseXSpeed:Boolean = true;
		public function ClMeteor(mainRef:Main) 
		{
			super(mainRef.stage);
			if (Main.useEmbededContent)
				LoadEmbededImage(Assets.Asteroid1);
			else
				LoadImage("../content/images/asteroid5.png");
				
			MeteorArray.push(this);
		}
		
		override protected function InitParams():void 
		{
			super.InitParams();
			
			switch(SizeType)
			{
				case TYPE_SMALL:
				scaleX = 0.3;
				scaleY = scaleX;
				xSpeed = (Math.random()-1)*6;
				hp = 2;
				speedPower = 8;
				rotationSpeed = (Math.random() * 9) + 3;
				break;
				case TYPE_NORMAL:
				scaleX = 0.6;
				scaleY = scaleX;
				xSpeed = (Math.random()-1)*4;
				hp = 4;
				speedPower = 6;
				rotationSpeed = (Math.random() * 6) + 3;
				if (Math.random() > 0.5)
					rotationSpeed *= -1;
				break;
				case TYPE_BIG:
				xSpeed = (Math.random()-1)*2;
				hp = 8;
				speedPower = 2;
				rotationSpeed = (Math.random() * 3) + 2;
				break;
			}
			
			rotation = Math.random() * 360;
			x = Math.random() * stageRef.stageWidth;
			y = -height;
			//if (UseXSpeed)
				//xSpeed = Math.random()*2;
			
			ySpeed = speedPower;
		}
		
		
		public function TakeHit(tar:ClObject):void
		{
			hp -= tar.hp;
			tar = null;
		}
		
		override public function removeSelf():void 
		{
			super.removeSelf();
			RemoveFromArray();
		}
		
		protected function RemoveFromArray():void
		{
			MeteorArray.splice(MeteorArray.indexOf(this), 1);
		}
		
		override protected function update(e:Event):void 
		{
			super.update(e);
			x += xSpeed;
			y += ySpeed;
			rotation += rotationSpeed;
			
			if (hp < 1)
			{
				new ClExplosion(this);
				removeSelf();
				return;
			}
			
			/* При захождении за груницу уничтожить (?)*/
			if (y > stage.stageHeight+height)
			{
				removeSelf();
				return;
				//if (RandomiseX)
					//x = Math.random() * stageRef.stageWidth;
				//y = -stageRef.stageHeight;
			}
			
			/* Столкновение с игроком */
			if (this.tarArea.hitTestObject(Main.player.tarArea))
			{
				Main.player.TakeHit(this);
				TakeHit(Main.player);
				//removeSelf();
				return;
			}
		}
	}

}